import { back, invoke, jump } from '../../router'
import http from '../../http'

export const mixin = {
  data () {
    return {
      loading: false,
      o: {}
    }
  },
  beforeRouteEnter (to, from, next) {
    http.post('/user/betDetail', {orderId: to.query.id}).then(o => {
      next(vm => {
        vm.o = o
      })
    })
  },
  methods: {
    refresh () {
      http.post('/user/betDetail', {orderId: this.o.id}).then(o => {
        this.loading = false
        this.$toast('刷新成功')
        this.o = o
      })
    },
    cancel () {
      http.post('/order/delOrder', {orderId: this.o.id}).then(res => {
        this.$toast(res.msg || '取消订单成功')
        setTimeout(back, 2000)
      })
    },
    pay () {
      var o = this.o
      if (this.app) {
        location = `checkout://orderId=${o.id}&amount=${o.amount}&lotteryType=${o.lotteryType}`
        return
      }
      http.post('/order/payOrder', {orderId: o.id}).then(() => {
        this.$store.dispatch('fetch')
        this.$dialog.alert({
          title: '支付成功',
          message: '该订单已支付成功',
          onConfirm: () => {
            this.$router.replace('/' + o.lotteryType)
          }
        })
      })
    },
    invoke,
    jump
  }
}
